/***********************************************************************************
 @file   
 @author
 @attention
 ***********************************************************************************/
#include "mid_gpio.h"
/***********************************************************************************
 * @global 
***********************************************************************************/


/***********************************************************************************
 * @brief  输出端口初始化
***********************************************************************************/
void gpio_out_init(void)
{
    stc_gpio_cfg_t stcGpioCfg;
    
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);  //打开GPIO外设时钟门控
      
     ///< 端口方向配置->输出
    stcGpioCfg.enDir = GpioDirOut;
    stcGpioCfg.enPu = GpioPuEnable;
    stcGpioCfg.enPd = GpioPdDisable;

    Gpio_Init(POW_PORT, POW_PIN, &stcGpioCfg);
    Gpio_SetIO(POW_PORT, POW_PIN); //开机电源 
    
    Gpio_Init(LED1_PORT, LED1_PIN, &stcGpioCfg);
    Gpio_ClrIO(LED1_PORT, LED1_PIN); ///< LED关闭

    Gpio_Init(LED2_PORT, LED2_PIN, &stcGpioCfg);
    Gpio_ClrIO(LED2_PORT, LED2_PIN); 

    Gpio_Init(LED3_PORT, LED3_PIN, &stcGpioCfg);
    Gpio_ClrIO(LED3_PORT, LED3_PIN);

    Gpio_Init(LED4_PORT, LED4_PIN, &stcGpioCfg);
    Gpio_ClrIO(LED4_PORT, LED4_PIN);    
       
    Gpio_Init(SENSOR_PORT, SENSOR_PIN, &stcGpioCfg);
    Gpio_ClrIO(SENSOR_PORT, SENSOR_PIN);
    
    Gpio_Init(ALARM_PORT, ALARM_PIN, &stcGpioCfg);
    Gpio_ClrIO(ALARM_PORT, ALARM_PIN); 

    Gpio_Init(LORA_M0_PORT, LORA_M0_PIN, &stcGpioCfg);
    Gpio_ClrIO(LORA_M0_PORT, LORA_M0_PIN);   
    
    Gpio_Init(LORA_M1_PORT, LORA_M1_PIN, &stcGpioCfg);
    Gpio_ClrIO(LORA_M1_PORT, LORA_M1_PIN);   
    
    Gpio_Init(VIR_PORT, VIR_PIN, &stcGpioCfg);
    Gpio_ClrIO(VIR_PORT, VIR_PIN); 
    
    #if(defined(HARD_WARE)&&(HARD_WARE==HARD_WARE_FIRST))
    #else
    Gpio_Init(WT588_CLK_PORT, WT588_CLK_PIN, &stcGpioCfg);
    Gpio_ClrIO(WT588_CLK_PORT, WT588_CLK_PIN); 
    Gpio_Init(WT588_DAT_PORT, WT588_DAT_PIN, &stcGpioCfg);
    Gpio_ClrIO(WT588_DAT_PORT, WT588_DAT_PIN);          
    #endif 

//    
    sensor_en(1); infrared_en(1); alarm_en(0);
    lora_m0(1);   lora_m1(1);
}
/***********************************************************************************
 * @brief  输入端口初始化 按键输入和未配置端口输入
***********************************************************************************/
void gpio_in_init(void)
{
    stc_gpio_cfg_t stcGpioCfg;   
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);  //打开GPIO外设时钟门控        
    ///< 端口方向配置->输入
    stcGpioCfg.enDir = GpioDirIn;
    stcGpioCfg.enDrv = GpioDrvL;
    stcGpioCfg.enPu  = GpioPuEnable;
    stcGpioCfg.enPd  = GpioPdDisable;
    stcGpioCfg.enOD  = GpioOdDisable;
    ///< 端口输入/输出值寄存器总线控制模式配置->AHB
    stcGpioCfg.enCtrlMode = GpioAHB;
    //---------------------------------------------按键输入上拉并配置下降沿中断
    Gpio_Init(KEY_EX_PORT, KEY_EX_PIN, &stcGpioCfg); 
    Gpio_Init(KEY_UP_PORT, KEY_UP_PIN, &stcGpioCfg); 
    Gpio_Init(KEY_RIGHT_PORT, KEY_RIGHT_PIN, &stcGpioCfg); 

    //按键下降沿中断
    Gpio_EnableIrq(KEY_EX_PORT, KEY_EX_PIN, GpioIrqFalling);
    EnableNvic(PORTB_IRQn, IrqLevel3, TRUE);
    //上升沿中断
    #if(defined(HARD_WARE)&&(HARD_WARE==HARD_WARE_FIRST))
    #else
    stcGpioCfg.enDrv = GpioDrvL;
    stcGpioCfg.enPu  = GpioPuEnable;//GpioPuDisable GpioPuEnable
    stcGpioCfg.enPd  = GpioPdDisable;   //GpioPdDisable 
    Gpio_Init(ADX_EXT_PORT, ADX_EXT_PIN, &stcGpioCfg);
    Gpio_EnableIrq(ADX_EXT_PORT, ADX_EXT_PIN, GpioIrqRising); // GpioIrqRising  GpioIrqFalling 
    EnableNvic(PORTD_IRQn, IrqLevel2, TRUE);          
    #endif

    //---------------------------------------------输入下拉未定义端口 
    stcGpioCfg.enPd = GpioPdEnable;
    stcGpioCfg.enPu = GpioPuDisable;
 
    #if(defined(HARD_WARE)&&(HARD_WARE==HARD_WARE_FIRST))
    Gpio_Init(GpioPortD, GpioPin4, &stcGpioCfg); 
    Gpio_Init(GpioPortD, GpioPin5, &stcGpioCfg); 
    Gpio_Init(GpioPortD, GpioPin6, &stcGpioCfg); 
    Gpio_Init(GpioPortD, GpioPin7, &stcGpioCfg); 
    Gpio_Init(GpioPortC, GpioPin13, &stcGpioCfg); 
    #else
    Gpio_Init(GpioPortB, GpioPin3, &stcGpioCfg); 
    Gpio_Init(GpioPortD, GpioPin5, &stcGpioCfg); 
    Gpio_Init(GpioPortC, GpioPin13, &stcGpioCfg);        
    #endif
}

/***********************************************************************************
* @brief  与普通IO分开，开机逻辑流畅
***********************************************************************************/
void swd_deinit(void)
{
    stc_gpio_cfg_t stcGpioCfg;   
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);  //打开GPIO外设时钟门控        
    ///< 端口方向配置->输入
    stcGpioCfg.enDir = GpioDirIn;
    stcGpioCfg.enDrv = GpioDrvL;
    stcGpioCfg.enPu  = GpioPuEnable;
    stcGpioCfg.enPd  = GpioPdDisable;
    stcGpioCfg.enOD  = GpioOdDisable;
    ///< 端口输入/输出值寄存器总线控制模式配置->AHB
    stcGpioCfg.enCtrlMode = GpioAHB;
    //----------------------------------------------配置SWD为普通IO口
    Sysctrl_SetFunc(SysctrlSWDUseIOEn, TRUE);
    Gpio_Init(GpioPortA, GpioPin13, &stcGpioCfg); 
    Gpio_Init(GpioPortA, GpioPin14, &stcGpioCfg); 
}
/***********************************************************************************
 * @brief  右上按键初始化 输入上拉  
***********************************************************************************/
void button_gpio_init(void)
{
    stc_gpio_cfg_t stcGpioCfg;
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);

    stcGpioCfg.enDir = GpioDirIn;
    stcGpioCfg.enDrv = GpioDrvL;
    stcGpioCfg.enPu = GpioPuEnable;//
    stcGpioCfg.enPd = GpioPdDisable;//
    stcGpioCfg.enOD = GpioOdDisable;
    Gpio_Init(KEY_UP_PORT, KEY_UP_PIN, &stcGpioCfg);
    Gpio_Init(KEY_RIGHT_PORT, KEY_RIGHT_PIN, &stcGpioCfg);
    
    #if(defined(HARD_WARE)&&(HARD_WARE==HARD_WARE_FIRST))
    #else
    stcGpioCfg.enDir = GpioDirOut;
    stcGpioCfg.enPu = GpioPuEnable;
    stcGpioCfg.enPd = GpioPdDisable;
    Gpio_Init(WT588_DAT_PORT, WT588_DAT_PIN, &stcGpioCfg);   
    Gpio_Init(WT588_CLK_PORT, WT588_CLK_PIN, &stcGpioCfg);        
    #endif

    //WT588

}
/***********************************************************************************
 * @brief  右上按键取消初始化 输入下拉
***********************************************************************************/
void button_gpio_deinit(void)
{
    stc_gpio_cfg_t stcGpioCfg;
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);

    stcGpioCfg.enDir = GpioDirIn;
    stcGpioCfg.enDrv = GpioDrvL;
    stcGpioCfg.enPu = GpioPuDisable;//
    stcGpioCfg.enPd = GpioPdEnable;//
    stcGpioCfg.enOD = GpioOdDisable;
    Gpio_Init(KEY_UP_PORT, KEY_UP_PIN, &stcGpioCfg);
    Gpio_Init(KEY_RIGHT_PORT, KEY_RIGHT_PIN, &stcGpioCfg);
    #if(defined(HARD_WARE)&&(HARD_WARE==HARD_WARE_FIRST))
    #else
    Gpio_Init(WT588_DAT_PORT, WT588_DAT_PIN, &stcGpioCfg);   
    Gpio_Init(WT588_CLK_PORT, WT588_CLK_PIN, &stcGpioCfg);        
    #endif
}
/***********************************************************************************
 * @brief  GPIO中断入口函数 只有功能键才能触发唤醒
***********************************************************************************/
void PortB_IRQHandler(void)
{  
    if(TRUE == Gpio_GetIrqStatus(KEY_EX_PORT, KEY_EX_PIN))
    {
        Gpio_ClearIrq(KEY_EX_PORT, KEY_EX_PIN);
    }
    sleep_device_operation_time(BUT_WAKEUP_TIME); 
}

#if(defined(HARD_WARE)&&(HARD_WARE==HARD_WARE_FIRST))
#else
void PortD_IRQHandler(void)
{  
    if(TRUE == Gpio_GetIrqStatus(ADX_EXT_PORT, ADX_EXT_PIN))
    {
        Gpio_ClearIrq(ADX_EXT_PORT, ADX_EXT_PIN);
//        printf("axl345 alarm adxl345.move_alarm\r\n");  
        adxl345.move_alarm = 1;
    }
    sleep_device_operation_time(BUT_WAKEUP_TIME); 
}
#endif




